Skip to content

Conversation

@michae2
Copy link
Collaborator

@michae2 michae2 commented Oct 25, 2025

sql, tree: move two formatting helper functions into tree

Move formatStatementHideConstants and formatStatementSummary from sql to tree so that we can call tree.FormatStatementHideConstants from the optbuilder package.

Epic: None

Release note: None


sql: implement EXPLAIN (FINGERPRINT) statement

This commit adds support for EXPLAIN (FINGERPRINT), a new EXPLAIN variant that returns statement fingerprints. Statement fingerprints are normalized forms of SQL statements where constants are replaced with underscores, making them useful for query pattern analysis and monitoring.

Key features:

  • Returns a single row with single string column containing statement fingerprint
  • Respects sql.stats.statement_fingerprint.format_mask cluster setting

Implementation details:

  • Added ExplainFingerprint mode to AST with validation
  • Implemented fingerprint computation during optbuild phase
  • Added telemetry tracking for usage
  • Comprehensive test coverage including edge cases and prepared statements

Examples:

  • EXPLAIN (FINGERPRINT) SELECT * FROM t WHERE a = 123 Returns: "SELECT * FROM t WHERE a = _"

Informs: #153633

Release note (sql change): Added EXPLAIN (FINGERPRINT) statement that returns normalized statement fingerprints with constants replaced by underscores.

🤖 Generated with Claude Code

@michae2 michae2 requested review from a team and DrewKimball October 25, 2025 05:47
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@michae2 michae2 force-pushed the explain-fingerprint branch from b7f9b41 to 7950a5f Compare October 29, 2025 18:33
@michae2
Copy link
Collaborator Author

michae2 commented Oct 29, 2025

After exploring it a bit, I don't think it's worth complicating things by implementing special behavior for EXPLAIN (FINGERPRINT) EXECUTE.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! :lgtm:

@yuzefovich reviewed 7 of 8 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball)


pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 166 at r2 (raw file):

query T
EXPLAIN (FINGERPRINT)
SELECT a, b, row_number() OVER (PARTITION BY b ORDER BY a) as rn

nit: let's use a window function that takes in some arguments and make some of those arguments constant.


pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 195 at r2 (raw file):

SELECT * FROM t WHERE a = _

# These two should have the same fingerprint (both should show "SELECT * FROM t WHERE a = _")

nit: seems like a leftover, or in the wrong spot.


pkg/sql/opt/optbuilder/explain.go line 34 at r2 (raw file):

		// so don't. This allows someone to run EXPLAIN (FINGERPRINT) for statements
		// they don't have permission to execute, for example. Instead, we create a
		// dummy empty VALUES clause as input

nit: missing period.


pkg/sql/opt/optbuilder/explain.go line 88 at r2 (raw file):

	}
	if explain.Mode == tree.ExplainFingerprint {
		stmtFingerprintFmtMask := tree.FmtHideConstants | tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&b.evalCtx.Settings.SV))

nit: should we extract formatStatementHideConstants into tree package and reuse here?

michae2 and others added 2 commits October 29, 2025 16:58
Move `formatStatementHideConstants` and `formatStatementSummary` from
sql to tree so that we can call `tree.FormatStatementHideConstants` from
the optbuilder package.

Epic: None

Release note: None
This commit adds support for EXPLAIN (FINGERPRINT), a new EXPLAIN
variant that returns statement fingerprints. Statement fingerprints are
normalized forms of SQL statements where constants are replaced with
underscores, making them useful for query pattern analysis and
monitoring.

Key features:
- Returns a single row with single string column containing statement
  fingerprint
- Respects sql.stats.statement_fingerprint.format_mask cluster setting

Implementation details:
- Added ExplainFingerprint mode to AST with validation
- Implemented fingerprint computation during optbuild phase
- Added telemetry tracking for usage
- Comprehensive test coverage including edge cases and prepared
  statements

Examples:
- EXPLAIN (FINGERPRINT) SELECT * FROM t WHERE a = 123
  Returns: "SELECT * FROM t WHERE a = _"

Informs: cockroachdb#153633

Release note (sql change): Added EXPLAIN (FINGERPRINT) statement that
returns normalized statement fingerprints with constants replaced by
underscores.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@michae2 michae2 force-pushed the explain-fingerprint branch from 7950a5f to c5ea6a2 Compare October 30, 2025 00:13
Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Nice work!

@DrewKimball reviewed 14 of 14 files at r3, 8 of 8 files at r4, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @michae2)


pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 218 at r4 (raw file):

# Test with cluster setting sql.stats.statement_fingerprint.format_mask set to 0
statement ok
SET CLUSTER SETTING sql.stats.statement_fingerprint.format_mask = 0

Should we include a notice when the setting is non-default?

Copy link
Collaborator Author

@michae2 michae2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFTRs!

bors r=yuzefovich,DrewKimball

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @DrewKimball)


pkg/sql/opt/optbuilder/explain.go line 34 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: missing period.

Done.


pkg/sql/opt/optbuilder/explain.go line 88 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: should we extract formatStatementHideConstants into tree package and reuse here?

Good point, done.


pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 166 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: let's use a window function that takes in some arguments and make some of those arguments constant.

Done.


pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 195 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: seems like a leftover, or in the wrong spot.

Done.


pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 218 at r4 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Should we include a notice when the setting is non-default?

If this cluster setting is already set, then system.statement_statistics should also be using it. I don't think we need to print a notice, we just need to have the same behavior for system.statement_statistics, system.statement_hints, and EXPLAIN (FINGERPRINT).

@craig
Copy link
Contributor

craig bot commented Oct 30, 2025

@craig craig bot merged commit bb8844a into cockroachdb:master Oct 30, 2025
25 checks passed
@michae2 michae2 deleted the explain-fingerprint branch October 30, 2025 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants